// pages/pay/pay.js
const util = require('../../utils/utils.js');
Page({

  /**
   * 页面的初始数据
   */
  data: {
   
    array: ['空调费', '电费'],
    objectArray: [
      {
        id: 0,
        name: '空调费'
      },
      {
        id: 1,
        name: '电费'
      }
    ],
    index: 0,
    userInfo:{},
    un_click1:'money_box',
    un_click2:'money_box',
    un_click3:'money_box',
    un_click4:'money_box',
    un_click5:'money_box',
    un_click6: 'money_box',
    money:0
  },
lv1:function(){
  this.setData({
    un_click1: 'money_box1',
    un_click2: 'money_box',
    un_click3: 'money_box',
    un_click4: 'money_box',
    un_click5: 'money_box',
    un_click6: 'money_box',
    money:10
  })
},
  lv2: function () {
    this.setData({
      un_click1: 'money_box',
      un_click2: 'money_box1',
      un_click3: 'money_box',
      un_click4: 'money_box',
      un_click5: 'money_box',
      un_click6: 'money_box',
      money:20
    })
  },
  lv3: function () {
    this.setData({
      un_click1: 'money_box',
      un_click2: 'money_box',
      un_click3: 'money_box1',
      un_click4: 'money_box',
      un_click5: 'money_box',
      un_click6: 'money_box',
      money:30
    })
  },
  lv4: function () {
    this.setData({
      un_click1: 'money_box',
      un_click2: 'money_box',
      un_click3: 'money_box',
      un_click4: 'money_box1',
      un_click5: 'money_box',
      un_click6: 'money_box',
      money:50
    })
  },
  lv5: function () {
    this.setData({
      un_click1: 'money_box',
      un_click2: 'money_box',
      un_click3: 'money_box',
      un_click4: 'money_box',
      un_click5: 'money_box1',
      un_click6: 'money_box',
      money:100
    })
  },
  lv6: function () {
    this.setData({
      un_click1: 'money_box',
      un_click2: 'money_box',
      un_click3: 'money_box',
      un_click4: 'money_box',
      un_click5: 'money_box',
      un_click6: 'money_box1',
      money:150
    })
  },
  //获取选择充值的类型
  bindPickerChange: function (e) {
    console.log('picker发送选择改变，携带值为', e.detail.value)
    //写入选择充值的类型
    this.setData({
      index: e.detail.value
    })
  },
  pay(){
    let that=this;
    //显示弹出框
    wx.showModal({
      title: '充值提示',
      content: '您确定要充值'+that.data.array[that.data.index]+that.data.money+'元吗？',
      showCancel: true,
      cancelText: '取消',
      cancelColor: '#000000',
      confirmText: '确定',
      confirmColor: '#000000',
      //点击确定后执行的函数
      success: function (res) {
        if (res.confirm) {
          //初始化数据库        
          const db = wx.cloud.database()
          var r = {};
          //获取当前时间
          var time = util.formatTime(new Date());
          //调用云函数
          wx.cloud.callFunction({
            name: 'userinfos',
          }).then(res=>{
            r=res['result'];
          //添加数据
          db.collection('tb_ACRecharge').add({
            // data 字段表示需新增的 JSON 数据
            data: {
              RechargeTime: time,
              RechargeQuota: that.data.money,
              sushe:r['sushe'],
              type:that.data.index,
              xm: r['xm']
              
            }
          }).then(res => {
              //获取宿舍电费余额
              db.collection('dormitory').where({
                hostel: r['sushe']
              }).get().then(res => {     
                that.setData({
                  sushedf:res.data[0]                  
                })
                console.log(r['xm'])
                //调用云函数
                wx.cloud.callFunction({
                  //云函数名
                  name: 'updatassdf',
                  //像云函数传递的数据
                  data: {
                    efees: that.data.sushedf['efees'],
                    acfee: that.data.sushedf['acfee'],
                    type: that.data.index,
                    sushe: r['sushe'],
                    money: that.data.money                    
                  }
                  
                }).then(console.log)
              })
           
            })
            })
//充值成功，返回提示消息，3秒后跳转主页
          wx.showToast({
            title: '充值成功,3秒后自动返回主页',
            icon: 'none',
            duration: 3000,
            mask: true,
            success: function (res) {
              setTimeout(function () {
                wx.switchTab({ url: '../home/home' })
              }, 3000);
            },
            fail: function (res) { },
            complete: function (res) { },
          })
        } else if (res.cancel) {
          console.log('用户点击取消')
        }
        },
      fail: function(res) {},
      complete: function(res) {},
    })
    
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    //获取用户信息
    wx.getUserInfo({
      success: res => {
        this.setData({
          avatarUrl: res.userInfo.avatarUrl,
          userInfo: res.userInfo
        })
      }
    })

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})